package com.galaxy.order.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.galaxy.common.mybatis.core.page.PageQuery;
import com.galaxy.common.mybatis.core.page.TableDataInfo;
import com.galaxy.order.domain.OrderCoupon;
import com.galaxy.order.domain.bo.OrderCouponBo;
import com.galaxy.order.domain.vo.OrderCouponVo;
import com.galaxy.order.mapper.OrderCouponMapper;
import com.galaxy.order.service.OrderCouponService;
import io.github.linpeilie.Converter;
import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单优惠券信息Service业务层处理
 */
@RequiredArgsConstructor
@Service
public class OrderCouponServiceImpl extends ServiceImpl<OrderCouponMapper, OrderCoupon> implements OrderCouponService {

    private final OrderCouponMapper orderCouponMapper;
    private final Converter converter;

    @DS("slave")
    @Override
    public TableDataInfo<OrderCouponVo> queryPageList(OrderCouponBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<OrderCoupon> lqw = buildQueryWrapper(bo);
        var page = orderCouponMapper.selectPage(pageQuery.build(), lqw);
        List<OrderCouponVo> records = page.getRecords().stream()
            .map(orderCoupon -> converter.convert(orderCoupon, OrderCouponVo.class))
            .collect(Collectors.toList());
        return new TableDataInfo<>(records, page.getTotal());
    }

    @DS("slave")
    @Override
    public List<OrderCouponVo> queryList(OrderCouponBo bo) {
        LambdaQueryWrapper<OrderCoupon> lqw = buildQueryWrapper(bo);
        return orderCouponMapper.selectList(lqw).stream()
            .map(orderCoupon -> converter.convert(orderCoupon, OrderCouponVo.class))
            .collect(Collectors.toList());
    }

    @DS("slave")
    @Override
    public OrderCoupon queryById(Long id) {
        return orderCouponMapper.selectById(id);
    }

    @Override
    public Boolean insertByBo(OrderCouponBo bo) {
        OrderCoupon add = converter.convert(bo, OrderCoupon.class);
        return orderCouponMapper.insert(add) > 0;
    }

    @Override
    public Boolean updateByBo(OrderCouponBo bo) {
        OrderCoupon update = converter.convert(bo, OrderCoupon.class);
        return orderCouponMapper.updateById(update) > 0;
    }

    @Override
    public Boolean deleteWithValidByIds(List<Long> ids, Boolean isValid) {
        if (isValid) {
            // TODO: Add validation logic here
        }
        return orderCouponMapper.deleteBatchIds(ids) > 0;
    }

    @NotNull
    private LambdaQueryWrapper<OrderCoupon> buildQueryWrapper(@NotNull OrderCouponBo bo) {
        LambdaQueryWrapper<OrderCoupon> lqw = Wrappers.lambdaQuery();
        lqw.like(bo.getOrderNo() != null, OrderCoupon::getOrderNo, bo.getOrderNo());
        lqw.eq(bo.getOrderId() != null, OrderCoupon::getOrderId, bo.getOrderId());
        lqw.eq(bo.getCouponId() != null, OrderCoupon::getCouponId, bo.getCouponId());
        return lqw;
    }

    @Override
    public void deleteByOrder(Long orderId, String orderNo) {
        LambdaQueryWrapper<OrderCoupon> lqw = Wrappers.lambdaQuery();
        lqw.eq(OrderCoupon::getOrderId, orderId);
        lqw.eq(OrderCoupon::getOrderNo, orderNo);
        orderCouponMapper.delete(lqw);
    }
}
